<template>
  <div class="main-panel">
    <div class="main-header">
      <div class="header-btn">
        <el-button type="primary" icon="el-icon-plus" @click="openDiaolog(null)">添加</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="listData"
      row-key="id"
      :border="true"
      :tree-props="{children: 'childrens'}"
      default-expand-all
      style="width: 100%">
      <el-table-column
        type="index"
        width="50" align="center"/>
      <el-table-column
        prop="name"
        label="部门名称"
        width="140">
        <template slot-scope="scope">
          <i :class="scope.row.iconCls"></i> {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column
        prop="code"
        label="编号"
        width="60" align="center"/>
      <el-table-column
        prop="seq"
        label="排序"
        width="140">
      </el-table-column>
      <el-table-column
        prop="iconCls"
        label="图标"
        width="240">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="200">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
      <el-table-column
        prop="action"
        label="操作"
        width="300"
        align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="openDiaolog(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteOrg(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="form.id?'修改':'添加'"
      :visible.sync="model" width="500px">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="编号" required>
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.seq" controls-position="right"></el-input-number>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="form.icon" @focus="iconsModel=true"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="上级部门">
          <el-select v-model="form.pid" filterable placeholder="请选择" value="">
            <el-option
              v-for="item in allOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="model = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">{{form.id?'修 改':'保 存'}}</el-button>
    </span>
    </el-dialog>
    <icons v-model="iconsModel" :on-select="(selected)=>{form.icon=selected}"/>
  </div>
</template>

<script>
import { getList, organizationAll, editOrganization, addOrganization, delOrganization } from '_api/organization'
import icons from '_c/diaolog/icons'
export default {
  components: {
    icons
  },
  created () {
    this.loadData()
  },
  data () {
    return {
      listLoading: true,
      listData: [],
      allOptions: [],
      form: {},
      model: false,
      iconsModel: false
    }
  },
  methods: {
    loadData () {
      getList().then(res => {
        this.listData = res
        this.listLoading = false
      })
    },
    openDiaolog (row) {
      if (row) {
        this.form = {
          id: row.id,
          name: row.name,
          code: row.code,
          seq: row.seq,
          pid: row.pid,
          address: row.address,
          icon: row.iconCls
        }
      } else {
        this.form = {
          seq: 0
        }
      }
      organizationAll({ id: this.form.id }).then(res => {
        this.allOptions = res
        this.model = true
      })
    },
    handleSubmit () {
      let self = this
      self.model = false
      if (this.form.id) {
        editOrganization(this.form).then(() => {
          self.$message({ message: '修改成功', type: 'success' })
          self.loadData()
        })
      } else {
        addOrganization(self.form).then(() => {
          self.$message({ message: '添加成功', type: 'success' })
          self.loadData()
        })
      }
    },
    deleteOrg (id) {
      let self = this
      self.$confirm('您是否要删除当前部门', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delOrganization({ id: id }).then(() => {
          self.$message({ message: '删除成功', type: 'success' })
          self.loadData()
        })
      })
    }
  }
}
</script>
